import traceback

from sqlalchemy import Column, String, Text, DateTime, JSON
from sqlalchemy.dialects.mysql import MEDIUMTEXT
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm.session import Session

from domain.consts import user_context
from domain.page import Page
from eshoputils.mysql_util import DataBase

Base = declarative_base()

class SpiderTask(Base):
    # 定义表名,和数据库一致
    __tablename__ = 'spider_task'
    # 定义字段
    id = Column(String(32), primary_key=True)
    task_configuration = Column(JSON)
    task_state = Column(String(100))
    task_name = Column(String(256))
    task_info = Column(MEDIUMTEXT)
    create_time = Column(DateTime)
    finish_time = Column(DateTime)
    user_id = Column(String(100))

    @classmethod
    def insert_task(cls, **kwargs):
        """
        插入task
        """
        session = Session(bind=DataBase.get_engin())
        try:
            images_injuries_task_entity = SpiderTask(**kwargs)
            session.add(images_injuries_task_entity)
            session.commit()
        except Exception as e:
            traceback.print_exc()
            session.rollback()
        finally:
            session.close()

    @classmethod
    def update_param_by_id(cls, task_id, update_param):
        """
        更新状态
        """
        session = Session(bind=DataBase.get_engin())
        try:
            session.query(SpiderTask).filter_by(id=task_id).update(update_param)
            session.commit()
        except Exception as e:
            traceback.print_exc()
            session.rollback()
        finally:
            session.close()

    @classmethod
    def select_task(cls,task_id):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask).filter_by(id=task_id)
            return  res
        finally:
            session.close()

    @classmethod
    def select_task_by(cls,task_state):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask).filter_by(task_state=task_state)
            return  res
        finally:
            session.close()

    @classmethod
    def select_list(cls):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask)
            return  res
        finally:
            session.close()

    @classmethod
    def select_page(cls, page_info:Page):
        """
        查询结果
        """
        offset = (page_info.current_page - 1) * page_info.page_size
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask).filter_by(user_id=user_context.get()).order_by(SpiderTask.create_time.desc()).offset(offset).limit(page_info.page_size).all()
            return  res
        finally:
            session.close()

    @classmethod
    def get_count(cls):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask).filter_by(user_id=user_context.get()).count()
            return  res
        finally:
            session.close()

    @classmethod
    def get_all_count(cls):
        """
                查询结果
                """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderTask).count()
            return res
        finally:
            session.close()




